package controllers.managers
{
	import flash.media.SoundChannel;
	
	import mx.logging.ILogger;
	
	import org.osmf.logging.Logger;
	
	import utils.LogUtil;

	/**
	 *  播放音频
	 *@author 曾康
	 *@date 2012-08-16
	 **/
	public class SoundPlayerService
	{
		public static var NAME : String = "SoundPlayer";
		public static var INST_NAME : String = "instSoundPlayer";
		
		public static var PLAY : Boolean = true;
		public static var STOP : Boolean = false;
		
		private var sound:alarmSound;
		private var soundChannel:SoundChannel;
		//默认播放声音
		private var isPlaySound:Boolean = false;
		private var logger:ILogger = null;
		
		public function SoundPlayerService()
		{
			if( sound == null )
			{
				sound = new alarmSound();
			}
			
			logger = LogUtil.getInst().getLogger(this);
			logger.info("初始化"+NAME);
		}
		
		/**
		 *  播放
		 **/
		public function play():void{
			try
			{
				if( soundChannel == null || soundChannel.position == 0)
				{
					//只有在停止的时候才能播放
					soundChannel = sound.play(0,10000000);
					isPlaySound = true;
					logger.info("播放报警音频。");
				}
			} 
			catch(error:Error) 
			{
				logger.error("播放音频失败。原因："+error.getStackTrace());
			}
		}
		
		/**
		 *  停止
		 **/
		public function stop():void{
			if(soundChannel)
			{
				try
				{
					soundChannel.stop();
					isPlaySound = false;
					soundChannel = null;
					logger.info("停止播放报警音频。");
				} 
				catch(error:Error) 
				{
					logger.error("停止播放音频失败。原因："+error.getStackTrace());
				}
				
			}
		}
		
		/**
		 * 是否播放
		 * true 则播放
		 * false 不播放
		 **/
		public function getState():Boolean
		{
			return isPlaySound;
		}
		
		/**
		 * 是否播放
		 **/
		public function setState(bool:Boolean):void
		{
			isPlaySound = bool;
		}
	}
}